package kk.yiyi.safedesign;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.mylhyl.superdialog.SuperDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import kk.yiyi.safedesign.account.LoginService;
import kk.yiyi.safedesign.base.Constants;
import kk.yiyi.safedesign.base.MessageEvent;
import kk.yiyi.safedesign.base.SharePresenterUtils;
import kk.yiyi.safedesign.databinding.ActivityLoginBinding;
import kk.yiyi.safedesign.gps.GpsUtils;
import kk.yiyi.safedesign.main.MainActivity;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding loginBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        loginBinding= ActivityLoginBinding.inflate(LayoutInflater.from(this));
        setContentView(loginBinding.getRoot());
        EventBus.getDefault().register(this);
        GpsUtils.getInstance().getGps(this);
        if(!SharePresenterUtils.getData(this, Constants.TABLE,"user").equals("")){
           Intent intent=new Intent(this,MainActivity.class);
           startActivity(intent);
           finish();
        }else {
            loginBinding.login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDialog();
                }
            });
            showDialog();
        }
    }

    public void sendCode(Context context) {
        RegisterPage page = new RegisterPage();
        //如果使用我们的ui，没有申请模板编号的情况下需传null
        page.setTempCode(null);
        page.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // 处理成功的结果
                    HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                    // 国家代码，如“86”
                    String country = (String) phoneMap.get("country");
                    // 手机号码，如“13800138000”
                    String phone = (String) phoneMap.get("phone");
                    // TODO 利用国家代码和手机号码进行后续的操作
                    Log.d("demo",phone);
                    LoginService loginService=new LoginService();
                    loginService.selectLogin(LoginActivity.this,phone);
                } else{
                    // TODO 处理错误的结果
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT);
                }
            }
        });
        page.show(context);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageEvent message) {

        if(message.getMessage().equals("login_success")){
           Intent intent=new Intent(LoginActivity.this, MainActivity.class);
           startActivity(intent);
           finish();
        }
        if(message.getMessage().equals("data")){
            Toast.makeText(LoginActivity.this,message.getmContent(),Toast.LENGTH_SHORT).show();
        }
    }

    private void showDialog(){
        new SuperDialog.Builder(this).setRadius(10)
                .setAlpha(0.9f)
                .setTitle("温馨提示").setMessage("使用该应用前，请先进行手机号登录操作")
                .setPositiveButton("确定", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        sendCode(LoginActivity.this);
                    }
                }).setNegativeButton("取消", null).build();
    }
    private String getSignValidString(byte[] paramArrayOfByte) throws NoSuchAlgorithmException {
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(paramArrayOfByte);
        return toHexString(localMessageDigest.digest());
    }

    public String toHexString(byte[] paramArrayOfByte) {
        if (paramArrayOfByte == null) {
            return null;
        }
        StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
        for (int i = 0; ; i++) {
            if (i >= paramArrayOfByte.length) {
                return localStringBuilder.toString();
            }
            String str = Integer.toString(0xFF & paramArrayOfByte[i], 16);
            if (str.length() == 1) {
                str = "0" + str;
            }
            localStringBuilder.append(str);
        }
    }

}
